package com.ztp.pc.gui.gtabpane;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;

import org.apache.mina.util.ConcurrentHashSet;


public class GTabButton extends JPanel {
	private static final ImageIcon CloseIconNormal = Util.getDefaultCloseIcon(false);
	private static final ImageIcon CloseIconEntered = Util.getDefaultCloseIcon(true);
	private final static Border normalBorder = BorderFactory.createEtchedBorder();
	private final static Border selectedBorder = BorderFactory.createLoweredSoftBevelBorder();
	private final static Border focusedBorder = BorderFactory.createLineBorder(Color.RED, 3);
	
	private Color defaultBckgr;
	private JLabel label;
	private JButton buttonClose;
	private String linkedCardId;
	
	boolean isSelected = false;
	boolean isFocused = false;
	
	private GTabButton (){}
	
	GTabButton(String title, String linkedCardId ){
		this(title);
		this.linkedCardId = linkedCardId;
	}
	
	GTabButton (String title){
		this.setLayout(new BoxLayout(this, BoxLayout.LINE_AXIS) );
		this.setBorder(normalBorder);
		//this.addMouseListener(tabButtonlistener);
		//this.addMouseMotionListener(dispatchToParent);
		
		label = new JLabel(title);
		label.setFont(label.getFont().deriveFont(Font.PLAIN));
		
		buttonClose = new JButton(CloseIconNormal);
		buttonClose.setFocusable(false);
		buttonClose.setMargin(new Insets(0, 0, 0, 0));
		//buttonClose.setBorder(Borders.EMPTY_BORDER);
		buttonClose.setBorder(BorderFactory.createEmptyBorder());
		buttonClose.setVisible(false);
		buttonClose.addMouseListener(closeBtnMouseListener);
		
		defaultBckgr = buttonClose.getBackground();
		
		this.add(Box.createRigidArea(new Dimension(5, 1)));
		this.add(label);
		this.add(Box.createRigidArea(new Dimension(5, 1)));
		this.add(buttonClose);
		this.add(Box.createHorizontalGlue());
		
		//debug();
	}
	
	void invokeSelect(){
		select();
		for(TabButtonActionListener tbal : tabButtonListeners) {
			if(tbal != null){
				tbal.onSelect(GTabButton.this,linkedCardId);
			}
		}
	}
	
	private void select(){
		this.setBorder(selectedBorder);
		buttonClose.setVisible(true);
		isSelected = true;
	}
	
	void deselect(){
		this.setBorder(normalBorder);
		buttonClose.setVisible(false);
		isSelected = false;
	}
	
	void enableFocus(){
		this.setBorder(focusedBorder);
		isFocused = true;
	}
	
	void disableFocus(){
		if(isSelected){
			this.setBorder(selectedBorder);
		}
		else{
			this.setBorder(normalBorder);
		}
	}
	
	String getLinkedCardId(){
		return linkedCardId;
	}
	
	
	
	//----------LISTENERS
	
	MouseListener tabBtnMouseListener = new MouseListener() {
		@Override
		public void mouseReleased(MouseEvent arg0) {
			if(isFocused){
				disableFocus();
			}
		}
		@Override
		public void mousePressed(MouseEvent e) {
			if(e.getButton() == MouseEvent.BUTTON1){
				invokeSelect();
			}
			/*if(arg0.getButton() == MouseEvent.BUTTON1 && onSelectListener != null){
				onSelectListener.onSelect(GTabButton.this);
				select();
			}*/
		}
		@Override
		public void mouseExited(MouseEvent e) {
		}
		@Override
		public void mouseEntered(MouseEvent e) {
		}
		@Override
		public void mouseClicked(MouseEvent e) {
			
		}
	};
	
	MouseListener closeBtnMouseListener = new MouseListener() {
		@Override
		public void mouseReleased(MouseEvent e) {
			buttonClose.setIcon(CloseIconNormal);
		}
		@Override
		public void mousePressed(MouseEvent e) {
			buttonClose.setIcon(CloseIconEntered);
		}
		
		@Override
		public void mouseExited(MouseEvent e) {
			buttonClose.setBackground(defaultBckgr);
		}
		
		@Override
		public void mouseEntered(MouseEvent e) {
			buttonClose.setBackground(Color.red);
		}
		@Override
		public void mouseClicked(MouseEvent e) {
			if(e.getButton() == MouseEvent.BUTTON1){
				for(TabButtonActionListener l : tabButtonListeners){
					if(l != null){
						l.onClose(GTabButton.this, linkedCardId);
					}
				}
			}
		}
	};
	
	interface TabButtonActionListener{
		public void onSelect(GTabButton gtb, String linkedComp);
		public void onClose(GTabButton gtb, String linkedComp);
	}
	private ConcurrentHashSet<TabButtonActionListener> tabButtonListeners = new ConcurrentHashSet<TabButtonActionListener>();
	public void addTabBtnActionListener(TabButtonActionListener l){
		tabButtonListeners.add(l);
	}
	public boolean removeTabBtnActionListener(TabButtonActionListener l){
		return tabButtonListeners.remove(l);
	}
	
	
	public void addCloseBtnListener(ActionListener l ){
		buttonClose.addActionListener(l);
	}
	
	public void debug(){
		this.setName(label.getText());
	}
}
